package com.yuncheng.spcyApi.utils;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.config.SpcyMinioUtils;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.sys.SysConfigConstant;
import com.yuncheng.spcyApi.entity.SysConfig;
import com.yuncheng.spcyApi.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import utils.Util;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.InputStream;
import java.util.List;

@Component
public class SendMailUtils {
    private static final Logger log = LoggerFactory.getLogger(SendMailUtils.class);

    @Resource
    private JavaMailSender mailSender;
    @Resource
    private BusinessConstant businessConstant;

    @Value("${spring.mail.username}")
    private String fromToName;

    @Resource
    private SpcyMinioUtils spcyMinioUtils;

    @Resource
    @Lazy
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    @Lazy
    private ISysConfigService sysConfigService;

    // 是否使用Minio
    public boolean isEnableMinio(){
        return false;
    }

    public void sendMail(String title, String content, String sendTo,String fileName,String fileURL){
        this.sendMail(title,  content,  sendTo, fileName, fileURL,false);
    }

    /**
     * 发送邮箱
     * @param fileName 文件名
     * @param content 邮箱内容
     * @param title 邮箱标题
     * @param fileURL 文件路径
     * @param sendTo 发送对象
     * @param isMinio 是否使用Minio
     */
    public void sendMail(String title, String content, String sendTo,String fileName,String fileURL,boolean isMinio){
        //        sendTo = "970301718@qq.com";
//        sendTo = "2503689478@qq.com";

        List<SysConfig> EMAIL_CONFIG_ENABLE = sysConfigService.list(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigCode, SysConfigConstant.EMAIL_CONFIG_ENABLE));
        String flag = "0";
        if (CollectionUtils.isNotEmpty(EMAIL_CONFIG_ENABLE)){
            flag = EMAIL_CONFIG_ENABLE.get(0).getConfigValue();
        }

        if (flag.equals("0")){
            List<SysConfig> EMAIL_DEFAULT_NUMBER = sysConfigService.list(new LambdaQueryWrapper<SysConfig>()
                    .eq(SysConfig::getConfigCode, SysConfigConstant.EMAIL_DEFAULT_NUMBER));
            if (CollectionUtils.isNotEmpty(EMAIL_DEFAULT_NUMBER)){
                sendTo = EMAIL_DEFAULT_NUMBER.get(0).getConfigValue();
            }else {
                sendTo = "2503689478@qq.com";
            }
        }

        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
            helper.setFrom(fromToName);
            helper.setTo(sendTo);
            String QZ = "["+BusinessConstant.Dw_NAME+"] ";
            helper.setSubject(QZ+title);
            content = QZ + content;
            helper.setText(content, true);

            // 是否上传附件
            if (StringUtils.isNotBlank(fileURL)) {
                if (!this.isEnableMinio() && isMinio) {

                    InputStream inputStream = spcyMinioUtils.getObject(localFileUploadUtils.getSqsxWjjlRelativeUrl(fileURL));
                    if (inputStream == null){
                        return;
                    }
                    File file = localFileUploadUtils.asFile(inputStream, Util.getWjHz(fileName));
                    helper.addAttachment(fileName, file);

                } else {

                    String fileUrl = localFileUploadUtils.getSqsxWjjlAbsUrl(fileURL);
                    log.info("发送邮箱文件路径：{}", fileUrl);
                    helper.addAttachment(fileName, new File(fileUrl));

                }
            }

            mailSender.send(message);
        }catch (MessagingException e) {
            log.error("发送邮件失败");
        }
    }
}
